package com.tulingxueyuan.mall.modules.pms.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tulingxueyuan.mall.common.api.CommonPage;
import com.tulingxueyuan.mall.common.api.CommonResult;
import com.tulingxueyuan.mall.dto.ProductAttributeCateDTO;
import com.tulingxueyuan.mall.modules.pms.model.PmsProductAttributeCategory;
import com.tulingxueyuan.mall.modules.pms.model.PmsProductCategory;
import com.tulingxueyuan.mall.modules.pms.service.PmsProductAttributeCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 产品属性分类表 前端控制器
 * </p>
 *
 * @author Miao
 * @since 2022-05-19
 */
@RestController
@RequestMapping("/productAttribute/category")
public class PmsProductAttributeCategoryController {

    @Autowired
    PmsProductAttributeCategoryService attributeCategoryService;


    /**
     * 查询商品类型 列表
     * Request URL: http://localhost:8099/productAttribute/category/list?pageNum=1&pageSize=5
     * Request Method: GET
     */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public CommonResult<CommonPage<PmsProductAttributeCategory>> getList(@RequestParam Integer pageNum,
                                                                         @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize){

        Page page=attributeCategoryService.list(pageNum,pageSize);
        return CommonResult.success(CommonPage.restPage(page));
    }


    /*
     * 初始化筛选属性级联数据
     * url:'/productAttribute/category/list/withAttr',
     * method:'get'
     * */
    @RequestMapping(value = "/list/withAttr",method = RequestMethod.GET)
    public CommonResult withAttr(){
        List<ProductAttributeCateDTO> list=attributeCategoryService.getListWithAttr();
        return CommonResult.success(list);
    }

    /**
     * 添加类型
     * Request URL: http://localhost:8099/productAttribute/category/create
     * Request Method: POST
     */
    @RequestMapping(value = "/create",method = RequestMethod.POST)
    public CommonResult create(PmsProductAttributeCategory pmsProductAttributeCategory){
        boolean result=attributeCategoryService.add(pmsProductAttributeCategory);
        if(result){
            return CommonResult.success(result);
        }
        else {
            return CommonResult.failed();
        }
    }

    /**
     * 商品类型删除
     * Request URL: http://localhost:8099/productAttribute/category/delete/11
     * Request Method: GET
     */
    @RequestMapping(value = "/delete/{id}",method = RequestMethod.GET)
    public CommonResult deleteById(@PathVariable Long id){

        boolean result=attributeCategoryService.removeById(id);
        if(result){
            return CommonResult.success(result);
        }
        else {
            return CommonResult.failed();
        }
    }

    /**
     * 编辑商品类型
     * Request URL: http://localhost:8099/productAttribute/category/update/12
     * Request Method: OPTIONS
     */
    @RequestMapping(value = "/update/{id}",method = RequestMethod.POST)
    public CommonResult update(PmsProductAttributeCategory pmsProductAttributeCategory){

        boolean result=attributeCategoryService.updateById(pmsProductAttributeCategory);
        if(result){
            return CommonResult.success(result);
        }
        else {
            return CommonResult.failed();
        }


    }



}

